package com.ruoyi.web.controller.materialplan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.*;
import com.ruoyi.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预算材料总计划Controller
 *
 * @author ruoyi
 * @date 2020-07-08
 */
@Controller
@RequestMapping("/ruoyi/plan")
public class SysMaterialPlanController extends BaseController {
    private String prefix = "ruoyi/plan";

    @Autowired
    private ISysMaterialPlanService sysMaterialPlanService;

    @Resource
    private ISysMatClassificationTypeService sysMatClassificationTypeService;

    @Resource
    private ISysMatDataTypeService sys;

    @Resource
    private ISysMatDetailsTypeService sysMatDetailsTypeService;

    @Resource
    private ISysMatTypeService iSysMatTypeService;

    @RequiresPermissions("ruoyi:plan:view")
    @GetMapping()
    public String plan() {
        return prefix + "/plan";
    }

    /**
     * 查询预算材料总计划列表
     */
    @RequiresPermissions("ruoyi:plan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMaterialPlan sysMaterialPlan) {
        startPage();
        List<SysMaterialPlan> list = sysMaterialPlanService.selectSysMaterialPlanList(sysMaterialPlan);
        return getDataTable(list);
    }

    /**
     * 查询预算材料总计划列表
     */
    @RequiresPermissions("ruoyi:plan:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long mId) {
        startPage();
        List<SysMaterialPlan> list = sysMaterialPlanService.selectMaterialsRequisitionesLists(mId);
        return getDataTable(list);
    }
    /**
     * 查询改项目下所有的材料类型
     */
    @RequiresPermissions("ruoyi:plan:list")
    @PostMapping("/like/{idss}")
    @ResponseBody
    public List<SysMaterialPlan> like(@PathVariable("idss") Long mId) {
        List<SysMaterialPlan> list = sysMaterialPlanService.selectMaterialsRequisitionesLists(mId);
        System.out.println(list.size());
//        list.stream().forEach(item->{
//            System.out.println();
//        });
        return list;
    }
    /**
     * 导出预算材料总计划列表
     */
    @RequiresPermissions("ruoyi:plan:export")
    @Log(title = "预算材料总计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMaterialPlan sysMaterialPlan) {
        List<SysMaterialPlan> list = sysMaterialPlanService.selectall(sysMaterialPlan);
        ExcelUtil<SysMaterialPlan> util = new ExcelUtil<SysMaterialPlan>(SysMaterialPlan.class);
        return util.exportExcel(list, "plan");
    }

    /**
     * 新增预算材料总计划
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存预算材料总计划
     */
    @RequiresPermissions("ruoyi:plan:add")
    @Log(title = "预算材料总计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMaterialPlan sysMaterialPlan) {
        return toAjax(sysMaterialPlanService.insertSysMaterialPlan(sysMaterialPlan));
    }

    /**
     * 修改预算材料总计划
     */
    @GetMapping("/edit/{matId}")
    public String edit(@PathVariable("matId") Integer matId, ModelMap mmap) {
        SysMaterialPlan sysMaterialPlan = sysMaterialPlanService.selectSysMaterialPlanById(matId);
        mmap.put("sysMaterialPlan", sysMaterialPlan);
        return prefix + "/edit";
    }

    /**
     * 修改保存预算材料总计划
     */
    @RequiresPermissions("ruoyi:plan:edit")
    @Log(title = "预算材料总计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMaterialPlan sysMaterialPlan) {
        return toAjax(sysMaterialPlanService.updateSysMaterialPlan(sysMaterialPlan));
    }

    /**
     * 删除预算材料总计划
     */
    @RequiresPermissions("ruoyi:plan:remove")
    @Log(title = "预算材料总计划", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysMaterialPlanService.deleteSysMaterialPlanByIds(ids));
    }


    /**
     *
     *查询材料类型
     */

    @GetMapping("/plans")
    @ResponseBody
    public List<SysMatType> plans(SysMatType sysMatType) {
        //查询材料类型表
        List<SysMatType> sysMatTypes = iSysMatTypeService.selectSysMatTypeList(sysMatType);
        //获取查询到的对象需要的字段
        return sysMatTypes;
    }



    /**
     * 菜单选择起使用  根据父类查找子类
     */

    @GetMapping("/like/{pname}")
    @ResponseBody
    public List<SysMatDataType> like(@PathVariable("pname") String pname) {
        //这里获取到了材料分类的名字用名字去查询英文
        String s = sysMatClassificationTypeService.selectYC(pname);
        //通过获取到英文名字去材料数据表查询和这个英文名有关的集合
        List<SysMatDataType> sysMatDataTypes = sys.selectYC(s);
        //获取查询到的对象需要的字段
        return sysMatDataTypes;
    }

    /**
     * 根据选择的材料数据查询拥有的材料详情
     */
    @GetMapping("/love/{type}")
    @ResponseBody
    public List<SysMatDetailsType> love(@PathVariable("type") String type) {
        //通过材料数据的名称去查询他的英文名字 然后通过查询到的英文名字去查询材料详情和他有关的名字
        return sysMatDetailsTypeService.selectSysMatDetailsTypematdatatypeYC(sys.selectmatdatatypeYC(type));
    }

    /**
     * 根据选择的材料数据查询拥有的材料详情
     */
    @GetMapping("/classmat/{aname}")
    @ResponseBody
    public List<SysMatClassificationType> classmat(@PathVariable("aname") String matclass) {
        //获取到材料类型的类型字段
        String a = iSysMatTypeService.a(matclass);
        List<SysMatClassificationType> sysMatClassificationTypes = sysMatClassificationTypeService.selectSysMatClassificationTypeLists(a);
        return sysMatClassificationTypes;
    }

}
